import tkinter
from tkinter import *
import requests
from requests import exceptions
from lxml import etree
import tkinter as tk


def head():
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:94.0) Gecko/20100101 Firefox/94.0',
        'Cookie': '__utmc=30149280; __utmc=223695111; bid=Cqkkd_P-tmQ;'
    }
    return headers


# 线路1
def biquge(wz, heads):
    url = 'https://www.bige7.com/s?so=go&q=' + wz  # 笔趣阁
    try:
        bi_quge = requests.get(url=url, headers=heads, allow_redirects=False, timeout=5)
    except exceptions.RequestException as e:
        w = str(e)
        i = w.find('port=', 0)
        return [['线路异常{}'.format(w[i:i + 8:1])], ['qq群：414969759'], ['请加群截图发群里'], ['接收数据出错']]

    else:
        bi_quge.encoding = 'utf-8'
        txt = etree.HTML(bi_quge.text)
        shuname = []
        by = []
        jianjie = []
        href = []

        # 分析数据
        for x in range(1, 100):
            shuname.append(txt.xpath('/html/body/div[4]/div/div/div[' + str(x) + ']/div/div[2]/h4/a/text()'))
            by.append(txt.xpath('/html/body/div[4]/div/div/div[' + str(x) + ']/div/div[2]/div[1]/text()'))
            jianjie.append(txt.xpath('/html/body/div[4]/div/div/div[' + str(x) + ']/div/div[2]/div[2]/text()'))
            href.append(txt.xpath('/html/body/div[4]/div/div/div[' + str(x) + ']/div/div[2]/h4/a/@href'))

        # 消除空列表
        shuname = [x for x in shuname if x != []]
        by = [x for x in by if x != []]
        jianjie = [x for x in jianjie if x != []]
        href = [x for x in href if x != []]

        # 数据调整格式为列表
        shunamelist = [''.join(x) for x in shuname]
        bylist = [''.join(x) for x in by]
        jianjielist = [''.join(x) for x in jianjie]
        hreflist = ['https://www.bige7.com' + ''.join(x) for x in href]
        return shunamelist, bylist, jianjielist, hreflist


def butzhagnjie(*a):
    print(a)


def bjkbut():
    name = bjk.get()
    heads = head()
    i = biquge(name, heads)  # 调用笔趣阁的抓包函数
    shuname = i[0]
    by = i[1]
    jianjie = i[2]
    href = i[3]
    for x in shuname:  # 生成控件
        # if x % 2 == 0:
        #     z = 'orange'
        # else:
        #     z = 'yellow'
        # Listbox1.insert(tkinter.END, len(shuname))
        Listbox1.insert(tkinter.ACTIVE, x)

    #     Button(root, text='{}'.format(''.join(i[0][x])), bg=z, relief=RIDGE, width=25, command=butzhagnjie).grid(
    #         row=x + 2, column=0)
    #     Button(root, text='{}'.format(''.join(i[1][x])), bg=z, relief=RIDGE, width=20, command=butzhagnjie).grid(
    #         row=x + 2, column=1,
    #         sticky=N + S)
    #     Button(root, text='介绍：{}'.format(''.join(i[2][x])), bg=z, relief=RIDGE, width=120, command=butzhagnjie).grid(
    #         row=x + 2, column=2,
    #         sticky=N + S)
    #     listbox1 = Listbox(root, width=50, selectmode=BROWSE).grid(row=4, column=0, sticky=N + S)



if __name__ == '__main__':
    root = Tk()
    root.title('爬取小说        线路异常或显示异常加QQ群：414969759')
    root.geometry('1400x700')
    Label(root, text='   输入你要获取的小说：', width=20).grid(row=1, column=0, sticky=N + S + W)
    bjk = Entry(root, text='0', width=20)
    bjk.grid(row=1, column=1)
    butt = Button(root, text='开始搜索', command=bjkbut)
    butt.grid(row=1, column=2, sticky=W)
    Listbox1 = Listbox(root, width=150, height=30, fg='red', selectmode=BROWSE)
    Listbox1.grid(row=4, column=0, sticky=N + S)


    root.mainloop()
